#include <iostream>
#include <set>
#define int long long
using namespace std;
const int inf = 0x7ffffffffffff;
int tc;
int n, m, K;
int a[100005], c[100005];
int lg2[100005], X[4];
string str[100005];
int getr(int x) { return lg2[x] + ((1 << lg2[x]) != x); }
int rec[400005];
void dfs(int x, int l, int r, int cur) {
    if (l == 1) 
        rec[r] = x;
    if (l == r) 
        return;
    int mid = (l + r) >> 1;
    dfs(x << 1, l, mid, cur - 1);
    dfs(x << 1 | 1, mid + 1, r, cur - 1);
}
set<int> st;
int ans[100005];
pair<int, int> dfs(int x, int l, int r, int cur, int sep, int v, int id) {
    if (l == r) 
        return make_pair(l, (l <= sep ? a[r] : (l == v ? inf : 0)));
    int mid = (l + r) >> 1;
    pair<int, int> al = dfs(x << 1, l, mid, cur - 1, sep, v, id * 2 - 1);
    pair<int, int> ar = dfs(x << 1 | 1, mid + 1, r, cur - 1, sep, v, id * 2);
    if (str[cur][id] == '1') 
        return ar.second >= cur ? ar : al;
    else 
        return al.second >= cur ? al : ar;
}
signed main() {
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    lg2[0] = -1;
    for (int i = 1; i <= 100000; i++) lg2[i] = lg2[i - 1] + ((i & (i - 1)) == 0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= m; i++) cin >> c[i];
    K = lg2[n] + ((1 << lg2[n]) != n);
    for (int i = 1; i <= K; i++) cin >> str[i], str[i] = ' ' + str[i];
    cin >> tc;
    dfs(1, 1, 1 << K, K);
    while (tc--) {
        cin >> X[0] >> X[1] >> X[2] >> X[3];
        for (int i = 1; i <= n; i++) a[i] ^= X[i & 3];
        int Ans = 0;
        for (int i = 1; i <= m; i++) {
            st.clear();
            int cur = getr(c[i]);
            st.insert(dfs(rec[1 << cur], 1, 1 << cur, cur, c[i], 0, 1).first);
            for (int j = c[i] + 1; j <= (1 << cur); j++) st.insert(dfs(rec[1 << cur], 1, 1 << cur, cur, c[i], j, 1).first);
            ans[i] = 0;
            for (auto v : st) ans[i] += v;
        }
        for (int i = 1; i <= m; i++) Ans ^= i * ans[i];
        cout << Ans << "\n";
        for (int i = 1; i <= n; i++) a[i] ^= X[i & 3];
    }
    return 0;
}